<?php
!defined('IN_FRAME') && die('404 Page');
class c_file extends Controller {
    
    public $arrManagerData = null;
    private $blnLogined = false;
    private $mdlAcl;
    
    public function __construct() {
        $mdlManager = Helper::loadModel('manager');
        $this->mdlAcl = Helper::loadModel('acl');
        if ($mdlManager->logined()) {
            $this->arrManagerData = $mdlManager->getSession();
            $this->blnLogined = true;
        }
    }
    
    public function index() {
        if (!$this->blnLogined) {
            Helper::jsLoadTo(HTTP_URL . '/index.php?m=admin&c=login', '请先登录!');
        }
        $strPath = isset($_GET['path']) && !empty($_GET['path']) ? $_GET['path'] : '/';
        if (isset($_GET['del']) && !empty($_GET['del'])) {
            if (!$this->mdlAcl->checkPermission($this->arrManagerData['group_name'], 'attachment', 'delete')) {
                Helper::jsLoadTo(HTTP_URL . '/index.php?m=admin', '你无权删除文件!');
            }
            if (file_exists(UPLOADS_PATH . $_GET['del'])) {
                unlink(UPLOADS_PATH . $_GET['del']);
                $strMsg = '删除成功!';
            } else {
                $strMsg = '文件不存在!';
            }
            Helper::jsLoadTo(HTTP_URL . '/index.php?m=admin&c=file&path=' . $strPath, $strMsg);
        } elseif (isset($_POST['new_name']) && !empty($_POST['new_name'])) {
            if (!$this->mdlAcl->checkPermission($this->arrManagerData['group_name'], 'attachment', 'update')) {
                Helper::jsLoadTo(HTTP_URL . '/index.php?m=admin', '你无权删除文件!');
            }
            $strMsg = '重命名失败!';
            if (isset($_POST['path_']) && !empty($_POST['path_'])) {
                if (isset($_POST['path']) && !empty($_POST['path'])) {
                    if (rename(UPLOADS_PATH . $_POST['path'], UPLOADS_PATH . $_POST['path_'] . $_POST['new_name'])) {
                        $strMsg = '重命名成功!';
                    }
                }
            }
            Helper::jsLoadTo(HTTP_URL . '/index.php?m=admin&c=file&path=' . $strPath, $strMsg);
        }
        if (!$this->mdlAcl->checkPermission($this->arrManagerData['group_name'], 'attachment', 'view')) {
            Helper::jsLoadTo(HTTP_URL . '/index.php?m=admin', '你无权查看文件!');
        }
        $arrAssign = array(
            'strTitle' => '附件管理',
            'arrListDir' => array(),
            'arrListFile' => array(),
            'strPath' => '/',
            'strRealPath' => UPLOADS_PATH
        );
        $strRealPath = UPLOADS_PATH . $strPath;
        if (is_dir($strRealPath)) {
            $hdl = opendir($strRealPath);
            $arrAssign['strPath'] = str_replace(UPLOADS_PATH, '', $strRealPath);
            $arrAssign['strRealPath'] = $strRealPath;
            while (($file = readdir($hdl)) !== false) {
                if (!in_array($file, array(".", "..", "index.html"))) {
                    if (is_dir($strRealPath . $file)) {
                        array_push($arrAssign['arrListDir'], $file . '/');
                    } else {
                        array_push($arrAssign['arrListFile'], $file);
                    }
                }
            }
        }
        $this->_display('admin/file_index.php', $arrAssign);
    }
    
    /**
     * 加水印的例子
     */
    public function waterMark() {
        $strPath = '/uploads/13/09/29/104006gsM8.jpg';
        $strMarkPng = '/uploads/13/09/29/133113k7x9.png';
        $objThumb = PhpThumbFactory::create(ROOT_PATH . $strPath);
        $objThumb->createWatermark(ROOT_PATH . $strMarkPng, 10, 10);
        $objThumb->save(ROOT_PATH . $strPath);
    }
    
    public function cut() {
        if (!$this->blnLogined) {
            Helper::jsLoadTo(HTTP_URL . '/index.php?m=admin&c=login', '请先登录!');
        } elseif (!$this->mdlAcl->checkPermission($this->arrManagerData['group_name'], 'attachment', 'update')) {
            Helper::jsCloseWindow('你无权限裁剪操作!');
        }
        $arrAssign = array(
            'strTitle' => '图片截取',
            'w' => isset($_GET['w']) && is_numeric($_GET['w']) ? intval($_GET['w']) : 0,
            'h' => isset($_GET['h']) && is_numeric($_GET['h']) ? intval($_GET['h']) : 0,
            'callbackJs' => isset($_GET['callback']) ? $_GET['callback'] : '',
            'path' => '',
            'blnNewFile' => isset($_GET['new'])
        );
        $strPath = (isset($_GET['path']) && !empty($_GET['path'])) ? $_GET['path'] : '';
        $strMsg = '';
        if (empty($strPath) || !file_exists(ROOT_PATH . $strPath)) {
            $strMsg = '文件路径错误!';
        } else {
            if (!getimagesize(ROOT_PATH . $strPath)) {
                $strMsg = '不是图片文件!';
            } else {
                $arrAssign['path'] = $strPath;
            }
        }
        if (isset($_POST['sub_btn'])) {
            $objThumb = PhpThumbFactory::create(ROOT_PATH . $strPath, array('resizeUp' => true));
            $objThumb->crop($_POST['x'], $_POST['y'], $_POST['w'], $_POST['h']);
            if ($arrAssign['w'] * $arrAssign['h'] > 0) {
                $objThumb->resize($arrAssign['w'], $arrAssign['h']);
            }
            $strTemp = ROOT_PATH . $strPath;
            if (isset($_POST['new_file']) && '1' == $_POST['new_file']) {
                $strTemp = Helper::getUploadFilePath('.jpg');
                $strTemp = $strTemp['path'] . '/' . $strTemp['name'];
            }
            $objThumb->save($strTemp);
            $strExtJs = '';
            if (!empty($arrAssign['callbackJs'])) {
                $strExtJs = 'window.opener.' . $arrAssign['callbackJs'] . '("' . str_replace(ROOT_PATH, '', $strTemp) . '");';
            }
            Helper::jsCloseWindow('截图已保存!', $strExtJs);
        } elseif (!empty($strMsg)) {
            Helper::jsCloseWindow($strMsg);
        } else {
            $this->_display('admin/file_cut.php', $arrAssign);
        }
    }
    
    public function upload_all() {
        $this->upload(true);
    }
    
    public function upload($blnAll = false) {
        $arrRe = array(
        	'error' => 1,
            'url' => '',
            'message' => '未登录!'
        );
        if ($this->blnLogined) {
            if (!$this->mdlAcl->checkPermission($this->arrManagerData['group_name'], 'attachment', 'add')) {
                $arrRe['message'] = '你无上传权限!';
            } else {
                $arrTemp = !$blnAll ? Helper::saveUploadedFile() : Helper::saveUploadedFile('img_file', null, null);
                if ($arrTemp['ok']) {
                    $arrRe['error'] = 0;
                    $arrRe['url'] = $arrTemp['path'];
                    $arrRe['message'] = '';
                } else {
                    $arrRe['message'] = '上传错误,请检查文件格式和大小!';
                }
            }
        }
        die(json_encode($arrRe));
    }
}